package com.onesports.intelligent.k12.polarlight.controller.common;

import cn.gjing.excel.executor.ExcelFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.onesports.intelligent.k12.polarlight.constant.ApiPathConstants;
import com.onesports.intelligent.k12.polarlight.domain.dto.report.FinanceQueryDTO;
import com.onesports.intelligent.k12.polarlight.domain.vo.report.FinanceVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.sys.NotifyVO;
import com.onesports.intelligent.k12.polarlight.service.order.OrderService;
import com.onesports.intelligent.k12.polarlight.service.order.PayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@RequestMapping(ApiPathConstants.WX_PAY)
@Api(tags = "微信支付")
@AllArgsConstructor
public class WxPayController {

    private final PayService payService;

    private final OrderService orderService;

    @PostMapping("/payNotify")
    @ApiOperation(value = "支付回调接口")
    public ResponseEntity payNotify(@RequestBody String notifyData, HttpServletRequest request) throws WxPayException, JsonProcessingException {
        NotifyVO notifyVO = payService.payNotify(StringUtils.trimToNull(notifyData), request);
        return new ResponseEntity(notifyVO, HttpStatus.OK);
    }

//    @PostMapping("/refundsNotify")
//    @ApiOperation(value = "退款回调接口")
//    public ResponseEntity refundsNotify(@RequestBody String notifyData, HttpServletRequest request) throws WxPayException {
//        payService.refundsNotify(StringUtils.trimToNull(notifyData), request);
//        NotifyVO notifyVO = new NotifyVO("SUCCESS", "成功");
//        return new ResponseEntity(notifyVO, HttpStatus.OK);
//    }

    @ApiOperation("导出")
    @GetMapping("/export")
    public void export(HttpServletResponse response, FinanceQueryDTO dto) throws UnsupportedEncodingException {
        List<FinanceVO> data = orderService.financeExport(dto,"d91585bb0eccad30c6b19ee8538ef0ff");
        ExcelFactory.createWriter(FinanceVO.class, response)
                .write(data).flush();
    }

}
